package com.work.washer.config;

import com.work.washer.model.Type;
import com.work.washer.model.User;
import com.work.washer.service.MachineService;
import com.work.washer.service.TypeService;
import com.work.washer.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.text.ParseException;

/**
 * @author cys
 * @date 2024/2/22 10:49
 */
@Component
public class InitData {

    @Autowired
    private TypeService typeService;

    @Autowired
    private UserService userService;

    @Autowired
    private MachineService machineService;

    @Bean
    public void initH2DuplicateData() throws ParseException {
        saveType();
        saveUser();
    }



    private void saveType() {
        Type type = new Type();
        try {
            boolean save = typeService.save(type);
            if (!save) {
                saveType();
            } else {
                typeService.removeById(type.getId());
            }
        } catch (Exception e) {
            System.out.println(e.getLocalizedMessage());
        }
    }


    private void saveUser() {
        User user = new User();
        try {
            boolean save = userService.save(user);
            if (!save) {
                saveUser();
            } else {
                userService.removeById(user.getId());
            }
        } catch (Exception e) {

        }
    }

}
